package com.coder.algorithm.offer_2;

import java.util.Arrays;

/**
 * 扑克牌中的顺子
 *
 * @author yuhushuan
 * @date 2020/7/3 18:16
 */
public class _61_1_ContinuousCards {
    private static boolean isContinuous(int[] numbers) {
        if (numbers == null || numbers.length < 1) {
            return false;
        }
        Arrays.sort(numbers);
        int countOfZero = 0;
        for (int i = 0; i < numbers.length; ++i) {
            if (numbers[i] == 0) {
                countOfZero++;
            } else {
                break;
            }
        }
        int small = countOfZero;
        int big = small + 1;

        int distance = 0;
        while (big < numbers.length) {
            if (numbers[small] == numbers[big]) {
                return false;
            } else {
                distance += numbers[big] - numbers[small] - 1;
            }
            small++;
            big++;
        }

        return distance <= countOfZero;
    }

    public static void main(String[] args) {
        // 功能测试
        // 一个或多个大小王
        print(new int[]{4, 3, 6, 2, 0});
        print(new int[]{4, 3, 7, 2, 0});
        // 没有大小王
        print(new int[]{4, 3, 6, 2, 5});
        print(new int[]{4, 3, 6, 1, 5});
        // 有对子
        print(new int[]{3, 3, 6, 2, 5});

        // 特殊输入
        print(null);
    }

    private static void print(int[] numbers) {
        System.out.println("数组：" + Arrays.toString(numbers)
                + " 是顺子：" + isContinuous(numbers));
        System.out.println();
    }
}
